﻿using System;
using System.Diagnostics;
using System.Reactive.Linq;
using System.Threading;
using Exercises.BingTranslationService;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Exercises
{
    [TestClass]
    public class ObservingAsyncMethod
    {
        [TestMethod]
        public void TestMethod()
        {
            var appId = "88E025E2D1E4C4CD930DD6C31AB82E3C49552CBB";
            var text = "Hello";
            var fromLanguage = "en";
            var toLanguage = "fr";
            var contentType = "text/plain";
            var category = "general";

            var proxy = new LanguageServiceClient();

            var getTranslation =
                Observable.FromAsyncPattern<string, string, string, string, string, string, string>(
                    proxy.BeginTranslate, proxy.EndTranslate);
            var result = getTranslation(appId, text, fromLanguage, toLanguage, contentType, category);

            result.Subscribe(
                onNext: translatedText => Debug.WriteLine(translatedText),
                onError: ex => Debug.WriteLine(ex.Message)
                );
            Debug.WriteLine("Translate {0} to {1}", text, toLanguage);
            Thread.Sleep(3000);
        }
    }
}